// 创建cookie工具对象
var CookieUtils = new Object();

// 创建一个最新日期
CookieUtils.expdate = new Date();

/**
 * 获得单个cookie的对应的值
 * @param {} offset
 * @return {}
 */
CookieUtils.getCookieVal = function(offset) {
	var endstr = document.cookie.indexOf(";", offset);
	
	if (endstr == -1){
		endstr = document.cookie.length;
	}
	
	return unescape(document.cookie.substring(offset, endstr));
}

/**
 * 得到名字为name的Cookie保存的字符串
 * 
 * @param name
 * @return
 */
CookieUtils.getCookie = function(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		
		if (document.cookie.substring(i, j) == arg){
			return CookieUtils.getCookieVal(j);
		}
		
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	
	return null;
}

/**
 * 设置Cookie
 * 
 * @param name
 *            Cookie名
 * @param value
 *            Cookie对应的值（当为""设置Cookie为空）
 * @param expires
 *            当前的Cookie时间
 * @param path
 * @param domain
 * @param secure
 * @return
 */
CookieUtils.setCookie = function(name, value, expires, path, domain, secure) {
	CookieUtils.expdate.setTime(CookieUtils.expdate.getTime() + (24 * 60 * 60 * 1000 * 365));
	
	document.cookie = name + "=" + escape(value)
			+ ((expires) ? "; expires=" + expires.toGMTString() : "")
			+ ((path) ? "; path=" + path : "")
			+ ((domain) ? "; domain=" + domain : "")
			+ ((secure) ? "; secure" : "");
}

/**
 * 删除指定的cookie
 * @param {} name
 */
CookieUtils.deleteCookie = function(name) {
	var newExpDate = new Date();
	newExpDate.setTime(CookieUtils.expdate.getTime() - (86400 * 1000 * 1));
	
	CookieUtils.setCookie(name, "", newExpDate, "/", null, false);
}